/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opgea.token.domain.model.PrimeFacesFileUploadBean;
import com.opgea.token.service.DestinationService;
import com.opgea.token.service.EmployeeService;
import com.opgea.token.util.FacesUtil;
import com.opgea.token.web.dto.DestinationDTO;
import com.opgea.token.web.dto.EmployeeDTO;

/**
 * 
 * @author Ramesh
 */
@Controller
@Scope(value = "request")
@ManagedBean(name = "employeeController")
@RequestScoped
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DestinationService destinationService;

    private EmployeeDTO employeeDTO = new EmployeeDTO();
    private List<EmployeeDTO> employeeList = new ArrayList<EmployeeDTO>();
    private List<DestinationDTO> destinationList = new ArrayList<DestinationDTO>();

    private UploadedFile file;

    @PostConstruct
    public void init() {
	Long companyId = FacesUtil.getSessionUserDataObject().getCompanyId();
	employeeList = employeeService.findAllByCompanyId(companyId);
	destinationList = destinationService.findAllByCompany(companyId);
    }

    public EmployeeDTO getEmployeeDTO() {
	return employeeDTO;
    }

    public void setEmployeeDTO(EmployeeDTO employeeDTO) {
	this.employeeDTO = employeeDTO;
    }

    public List<EmployeeDTO> getEmployeeList() {
	return employeeList;
    }

    public void setEmployeeList(List<EmployeeDTO> employeeList) {
	this.employeeList = employeeList;
    }

    public List<DestinationDTO> getDestinationList() {
	return destinationList;
    }

    public void setDestinationList(List<DestinationDTO> destinationList) {
	this.destinationList = destinationList;
    }

    public String save() {
	Long companyId = FacesUtil.getSessionUserDataObject().getCompanyId();
	employeeDTO.setCompanyId(companyId);
	employeeDTO = employeeService.update(employeeDTO);
	if (employeeDTO.getId() > 0) {
	    FacesUtil.addMessage("messages", FacesMessage.SEVERITY_INFO,
		    employeeDTO.getFirstName() + " saved successfully!", null);
	    return "create.xhtml";
	} else {
	    FacesUtil.addMessage("messages", FacesMessage.SEVERITY_INFO,
		    employeeDTO.getFirstName() + " saved successfully!", null);
	    return "new.xhtml";
	}
    }

    public String edit() {
	String employeeId = FacesUtil.getRequestParameter("employeeId");
	employeeDTO = employeeService.find(new Long(employeeId));
	return "create.xhtml";
    }

    public String uploadImage() {
	String employeeId = FacesUtil.getRequestParameter("employeeId");
	employeeDTO = employeeService.find(new Long(employeeId));
	return "uploadImage.xhtml";
    }

    public void uploadDocument(FileUploadEvent fe) {
	file = fe.getFile();
	PrimeFacesFileUploadBean pfFileUploadBean = new PrimeFacesFileUploadBean();
	pfFileUploadBean.setFile(file);
	System.out.println(employeeDTO);
	try {
	    employeeService.uploadDocument(employeeDTO.getId(),
		    pfFileUploadBean);
	    FacesUtil.addMessage("messages", FacesMessage.SEVERITY_INFO,
		    file.getFileName() + ", uploaded successfully!", null);
	} catch (Exception e) {
	    FacesUtil.addMessage("messages", FacesMessage.SEVERITY_INFO,
		    e.getMessage(), null);
	}
    }
}
